package com.onenine.app.activity;

import android.graphics.drawable.Drawable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.onenine.app.R;
import com.onenine.app.fragment.FriendAttentionFragment;
import com.onenine.app.utils.RoleResLoader;
import com.onenine.base.base.BaseActivity;
import com.onenine.base.tab.CommonTabLayout;
import com.onenine.base.tab.listener.CustomTabEntity;
import com.onenine.base.tab.listener.OnTabSelectListener;
import com.onenine.base.tab.utils.TextTabEntity;
import com.onenine.common.util.ActivityUtils;
import com.onenine.common.util.ApUtils;
import com.onenine.common.util.SizeUtils;

import java.util.ArrayList;

/**
 * 创建时间：2018/6/12 0012 15:19
 * 创建人：fly
 * 功能描述：我的联系人
 */
public class MyContactActivity extends BaseActivity implements View.OnClickListener
        , OnTabSelectListener, ViewPager.OnPageChangeListener {

    private CommonTabLayout mTabLayout;
    private ViewPager mViewPager;
    private View mBtnBack, mBtnAdd;
    private PopupWindow mPopupWindow;

    @Override
    protected int getLayout() {
        return R.layout.activity_my_contact;
    }

    @Override
    protected void initViews() {
        super.initViews();
        mTabLayout = findViewById(R.id.tl_tab);
        mViewPager = findViewById(R.id.vp_pager);
        mBtnBack = findViewById(R.id.btn_back);
        mBtnAdd = findViewById(R.id.btn_add);
    }

    @Override
    protected void initListeners() {
        mBtnBack.setOnClickListener(this);
        mBtnAdd.setOnClickListener(this);
        mTabLayout.setOnTabSelectListener(this);
        mViewPager.addOnPageChangeListener(this);
    }

    @Override
    protected void initData() {
        ArrayList<CustomTabEntity> tabEntityList = new ArrayList<>();
        String[] titles = getResources().getStringArray(R.array.contact_tab_title);
        for (String title : titles) {
            tabEntityList.add(new TextTabEntity(title));
        }
        mViewPager.setAdapter(new MyContactFragmentAdapter(getSupportFragmentManager(), titles.length));
        mTabLayout.setTabData(tabEntityList);
        mTabLayout.setTextSelectColor(RoleResLoader
                .getRoleColorResourceId(RoleResLoader.DynamicResName.THEME_COLOR));
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_back) {
            finish();
        } else if (v.getId() == R.id.btn_add) {
            if (mPopupWindow != null) {
                mPopupWindow.dismiss();
                return;
            }
            TextView contentView = new TextView(this);
            contentView.setText(R.string.create_group_chat);
            contentView.setTextColor(ActivityCompat.getColor(ApUtils.getApp(), R.color.c333));
            contentView.setGravity(Gravity.CENTER);
            Drawable drawable = ActivityCompat.getDrawable(ApUtils.getApp(), R.drawable.ic_up_popup_bg);
            contentView.setBackground(drawable);
            mPopupWindow = new PopupWindow(contentView
                    , drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
            mPopupWindow.setOutsideTouchable(true);
            mPopupWindow.setFocusable(true);
            mPopupWindow.setTouchable(true);
            mPopupWindow.setAnimationStyle(R.style.TopRightScalePopWin);
            mPopupWindow.showAsDropDown(mBtnAdd, -(drawable.getIntrinsicWidth()
                    - mBtnAdd.getMeasuredWidth()), SizeUtils.dp2px(10));
            backgroundAlpha(0.5f);
            mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    backgroundAlpha(1.0f);
                    mPopupWindow = null;
                }
            });
            contentView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mPopupWindow.dismiss();
                    ActivityUtils.startActivity(MyContactActivity.this, GroupCreateActivity.class);
                }
            });
        }
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        Window window = getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.alpha = bgAlpha;
        window.setAttributes(lp);
    }

    @Override
    public void onTabSelect(int position) {
        mViewPager.setCurrentItem(position);
    }

    @Override
    public void onTabReselect(int position) {

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        mTabLayout.setCurrentTab(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    private class MyContactFragmentAdapter extends FragmentStatePagerAdapter {

        private int mTabCount;

        public MyContactFragmentAdapter(FragmentManager fm, int tabCount) {
            super(fm);
            mTabCount = tabCount;
        }

        @Override
        public int getCount() {
            return mTabCount;
        }

        @Override
        public Fragment getItem(int position) {
            return FriendAttentionFragment.newInstance(position);
        }
    }

}
